﻿//功能描述：绩效管理：奖金
using Hong.IRepository.IRepository_AssessmentManagement;
using Hong.Model.SysDomain.Model_AssessmentManagement;
using Hong.Model.SysDomain.Model_AssessmentManagement.ViewBusn;
using Hong.Repository.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Hong.Common.Encryption;
using Hong.Model.SysDomain.Model_AssessmentManagement.ViewModel;
using Hong.Model.SysDomain.Model_SystemSettings;

namespace Hong.Repository.Repository.Repository_AssessmentManagement
{
    public class AssBonusRepository: IAssBonusRepository
    {
        #region 构造器注入
        private readonly T_HongShi_3Context _dal;
        //注入解密SQL连接字符串
        private readonly SecurityStatic _securityStatic;
        public AssBonusRepository(T_HongShi_3Context dal, SecurityStatic securityStatic)
        {
            _dal = dal;
            _securityStatic = securityStatic;
        }
        #endregion


        #region 项目奖金调整
        /// <summary>
        /// 项目奖金调整
        /// </summary>
        /// <returns></returns>
        public List<AssessmentList> GetAssessmentList()
        {   
            var list = _dal.AssessmentLists.ToList();
            return list;
        }

        #region 查看
        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="DepId"></param>
        /// <returns></returns>
        public List<ViewBusnPerDep> GetViewBusnPerDep(int? DepId,string AssId)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = $"select * from AssessmentList a join AssBonus b on a.AssId=b.AssId join Personnel c on b.PersonnelId=c.PersonnelId join Department d on c.DepartmentId=d.depId join Dictionaries e on c.PersonneIMajor=e.dicId where a.AssId='{AssId}' ";
                if (DepId != null)
                {
                    sql += $" and e.dicId={DepId}";
                }
                var list= conn.Query<ViewBusnPerDep>(sql);
                return list.ToList();
            }
        }

        #endregion
        #endregion

        #region 部门奖金分配
        /// <summary>
        /// 部门奖金分配
        /// </summary>
        /// <returns></returns>
        public List<BounDepListView> GetBounDepListView(string AssId)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = "select * from Bonuscalculation a join AssessmentList b on a.AssId=b.AssId join Department c on a.DepId=c.DepId where 1=1 ";
                if (!string.IsNullOrEmpty(AssId))
                {
                    sql += $" and a.AssId='{AssId}'";
                }
                var list = conn.Query<BounDepListView>(sql);
                return list.ToList();
            }
        }
        #endregion

        #region 部门内奖金计算
        /// <summary>
        /// 部门内奖金计算显示
        /// </summary>
        /// <param name="AssId"></param>
        /// <returns></returns>
        public List<ViewDeptListBounS1> GetDepList(string AssId)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = "select * from BonuscalculationS1 a join AssessmentList b on a.AssId=b.AssId join Department c on a.DepId=c.DepId where 1=1 ";
                if (!string.IsNullOrEmpty(AssId))
                {
                    sql += $" and b.AssId='{AssId}'";
                }
                var list = conn.Query<ViewDeptListBounS1>(sql);
                return list.ToList();
            }
        }
        #endregion
        #region 部门奖金分配详情
        /// <summary>
        /// 部门奖金分配详情显示查询
        /// </summary>
        /// <param name="DepName"></param>
        /// <param name="AssId"></param>
        /// <returns></returns>
        public List<ViewBounDetailLIst> GetmentDep(int? DepId, string AssId)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = "select * from AssessmentList a join BonusDetails b on a.AssId=b.AssId join Department c on b.DepId=c.depId where 1=1 ";
                if (DepId!=null)
                {
                    sql += $" and c.DepId={DepId}";
                }
                if (!string.IsNullOrEmpty(AssId))
                {
                    sql += $" and a.AssId='{AssId}'";
                }
                var list = conn.Query<ViewBounDetailLIst>(sql);
                return list.ToList();
            }
        }
        #region 部门
        /// <summary>
        /// 部门
        /// </summary>
        /// <returns></returns>
        public List<Department> GetDepartments()
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = "select * from Department";
                var list = conn.Query<Department>(sql);
                return list.ToList();
            }
        }
        #endregion
        #endregion
        #region 年度总奖金
        /// <summary>
        /// 年度总奖金显示查询
        /// </summary>
        /// <returns></returns>
        public List<ViewAnnDepList> GetViewAnnDepList(int? DepId, string AssId)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = "select * from Annualdistribution a join AssessmentList b on a.AssId=b.AssId join Department c on a.DepId=c.depId where 1=1 ";
                if (DepId != null)
                {
                    sql += $" and c.DepId={DepId}";
                }
                if (!string.IsNullOrEmpty(AssId))
                {
                    sql += $" and a.AssId='{AssId}'";
                }
                var list = conn.Query<ViewAnnDepList>(sql);
                return list.ToList();
            }
        }
        #endregion
    }
}
